package bean;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.renderable.ParameterBlock;
import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Enumeration;

import javax.imageio.ImageIO;
import javax.media.jai.JAI;
import javax.media.jai.RenderableOp;
import javax.media.jai.RenderedOp;

import com.oreilly.servlet.MultipartRequest;
import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;

public class BoardDTO {
	private BoardVO vo = null;
	private MemberVO vo2 = null;
	private Connection conn = null;

	int listSize = 10; // 페이지당 표시되는 행수
	/*int blockSize = 5; // 한블럭당 표시되는 페이지 수
	
	int totPage = 0; // 전체 페이지 수
	int totBlock = 0; // 전체 블럭 수
	
	int nowBlock = 1; // 현재 블럭
	int startNo = 0; // 리스트 목록의 시작 위치
	int endNo = 0; // 리스트 목록의 끝 위치
	int startPage = 0; // 현재 블럭에서 표시되는 시작 블럭 번호
	int endPage = 0; // 현재 블럭에서 표시되는 끝 블럭 번호*/
	int totSize = 0; // 전체 게시물의 갯수
	int nowPage = 3; // 현재 페이지
	int pageNo = 0;  // 페이지 보여주는거
	
	public BoardDTO() {
		conn = new MyOracle().getConn();
	}
	
	public BoardVO getVo() {
		return vo;
	}
	
	public boolean insert() {
		boolean flag = false;
		try {
			String sql = "insert into board_(b_rno, m_id, b_text,b_date) values(seq_board__rno,?,?,sysdate)";
			PreparedStatement psmt = conn.prepareStatement(sql);
			psmt.setString(1, vo.getM_id());
			psmt.setString(2, vo.getB_text());
			 
			//vo.getB_text()
			 
			int affectedRow = psmt.executeUpdate();
			if (affectedRow > 0) {
				flag = true;
			}
			psmt.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	public void setVO(BoardVO vo) {
		this.vo = vo; // multipartrequest를 여기서 생성한다.
//		int size = 1024 * 1024 * 10;
//		String saveDir = "C:/Javaworkspace/jsp1024_1/WebContent/memberPhoto";
//		String sysFile = "";
//		MultipartRequest multi = null;
//
//		try {
//			if (vo.getRequest() != null) {
//				multi = new MultipartRequest(vo.getRequest(), saveDir, size,
//						"utf-8", new DefaultFileRenamePolicy());
//
//				Enumeration files = multi.getFileNames();
//				String tempFile = (String) files.nextElement();
//
//				sysFile = multi.getOriginalFileName(tempFile);
//
//				vo.setM_id(multi.getParameter("m_id"));
//				vo.setB_rno(Integer.parseInt(multi.getParameter("b_rno")));
//				vo.setB_text(multi.getParameter("b_text"));
//				vo.setB_date(multi.getParameter("b_date"));
//				
//				vo.setPhoto(sysFile);
//			
//				ParameterBlock pb= new ParameterBlock();
//				pb.add(saveDir + "/" + sysFile);
//				RenderedOp rOp = JAI.create("fileload", pb);
//				
//				BufferedImage bi = rOp.getAsBufferedImage();
//				BufferedImage thumb = new BufferedImage(100, 100,BufferedImage.TYPE_INT_RGB);
//				Graphics2D g=thumb.createGraphics();
//				g.drawImage(bi,0,0,100,100,null);
//				
//				File file = new File(saveDir + "/" + "sm_" + sysFile);
//				ImageIO.write(thumb, "png", file);
//			}
//		}
//		catch (Exception e) {
//			e.printStackTrace();
//		}
	}

	public void page(){
		String sql = "select count(*) cnt from board_ "
				+ "where m_id like ?";
		try {
			PreparedStatement pst = conn.prepareStatement(sql);
			pst.setString( 1, "%"+vo.getFind()+"%" );
			
			ResultSet rs = pst.executeQuery();
			
			if(rs.next()){
				
				nowPage = vo.getNowPage();
				totSize = rs.getInt("cnt");
				pageNo = totSize-nowPage;
				
				System.out.println("nowPage : " + nowPage);
				System.out.println("totSize : " + totSize);
				System.out.println("pageNo : " + pageNo);
			}
		} 
		catch (Exception e) {
			
		}
	}
	
	public ArrayList<BoardVO> select(){
		page();
		ArrayList<BoardVO> al = new ArrayList<>();
		System.out.println("SQL시작");
		String sql = "select * from("
				+"		select rownum rn, c.* from("
				+"		select * from board_ "
				+"		  where b_rno like ?)c"
				+"	)b"
				+" where b.rn between ? and ?"
				+" order by b.rn desc";
		try{
			PreparedStatement pst = conn.prepareStatement(sql);
			pst.setString(1, "%"+vo.getFind()+"%");
			pst.setInt(2, pageNo);
			pst.setInt(3, totSize);
			ResultSet rs = pst.executeQuery();
			int cnt = 0;
			while( rs.next() ){
				cnt++;
				BoardVO v = new BoardVO();
					v.setM_id(rs.getString("m_id"));
					v.setB_rno(rs.getInt("b_rno"));
					v.setB_text(rs.getString("b_text"));
					v.setB_date(rs.getString("b_date"));
				al.add(v);
			}
			System.out.println(cnt);
			rs.close();
			pst.close();
			
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
		return al;
	}
	
	public BoardVO selectOne(){
		BoardVO v = new BoardVO();
		String sql = "select * from board_ where m_id = ?";

		try {
			PreparedStatement psmt = conn.prepareStatement(sql);
			psmt.setInt(1, vo.getB_rno());
			ResultSet rs = psmt.executeQuery();

			if (rs.next()) {
				 v.setM_id(rs.getString("m_id"));
				 v.setB_rno(rs.getInt("b_rno"));
				 v.setB_text(rs.getString("b_text"));
				 v.setB_date(rs.getString("b_date"));
			}
			rs.close();
			psmt.close();
			
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return v;
	}
	
	public MemberVO getVo2() {
		return vo2;
	}

	public void setVo2(MemberVO vo2) {
		this.vo2 = vo2;
	}

	public void setVo(BoardVO vo) {
		this.vo = vo;
	}

	public int getListSize() {
		return listSize;
	}

	public void setListSize(int listSize) {
		this.listSize = listSize;
	}

	public int getNowPage() {
		return nowPage;
	}

	public void setNowPage(int nowPage) {
		this.nowPage = nowPage;
	}
	
	public int getTotSize() {
		return totSize;
	}
	
	public void setTotSize(int totSize) {
		this.totSize = totSize;
	}
}